<template>
	<div class="system-edit-dept-container">
		<el-dialog :title="(ruleForm.id!==0?'修改':'添加')+'部门'" v-model="isShowDialog" width="769px">
			<el-form ref="formRef" :model="ruleForm" :rules="rules" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="上级店铺">
							<el-cascader
								:options="list"
								:props="{ checkStrictly: true,emitPath: false, value: 'id', label: 'name' }"
								placeholder="请选择店铺"
								clearable
								class="w100"
								v-model="ruleForm.up_id"
							>
								<template #default="{ node, data }">
									<span>{{ data.name }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="部门名称" prop="name">
							<el-input v-model="ruleForm.name" placeholder="请输入名称" clearable></el-input>
						</el-form-item>
					</el-col>
					
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ruleForm.id!==0?'修 改':'添 加'}}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import {reactive, toRefs, defineComponent, getCurrentInstance,ref,unref} from 'vue';
import {edit, getList} from "/@/api/zmm/shop";
import {ElMessage} from "element-plus";

// 定义接口来定义对象的类型
interface TableDataRow {
	name: string;
	id: number;
  up_id:number;
	children?: TableDataRow[];
}
interface RuleFormState {
  id:number;
	up_id: number;
	name: string;

}
interface DeptSate {
	isShowDialog: boolean;
	ruleForm: RuleFormState;
	list: Array<TableDataRow>;
  rules: object;
}

export default defineComponent({
	name: 'zmmEditShop',
	setup(prop,{emit}) {
    const {proxy} = getCurrentInstance() as any;
    const formRef = ref<HTMLElement | null>(null);
		const state = reactive<DeptSate>({
			isShowDialog: false,
			ruleForm: {
        id:0,
        up_id: 0, // 上级部门
				name: '', // 部门名称

			},
			list: [], // 部门数据
      rules: {
        name:[
          {required: true, message: "部门名称不能为空", trigger: "blur"},
        ]
      }
		});
		// 打开弹窗
		const openDialog = (row?: RuleFormState|number) => {
      resetForm()
      getList().then((res:any)=>{
		const list = { id: 0, name: '主店铺', children: [] };
		list.children = proxy.handleTree(res.data.zmm_shop_list??[], "id","up_id");
          state.list=new Array(list) as any;
      });
      if(row && typeof row === "object"){
        state.ruleForm = row;
      }else if(row && typeof row === 'number'){
        state.ruleForm.up_id = row
      }
			state.isShowDialog = true;
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = () => {
      const formWrap = unref(formRef) as any;
      if (!formWrap) return;
      formWrap.validate((valid: boolean) => {
        if (valid) {
        //   if(state.ruleForm.deptId===0){
        //     //添加
        //     addDept(state.ruleForm).then(()=>{
        //       ElMessage.success('角色添加成功');
        //       closeDialog(); // 关闭弹窗
        //       emit('deptList')
        //     });
        //   }else{
            //修改
            edit(state.ruleForm).then(()=>{
              ElMessage.success('修改成功');
              closeDialog(); // 关闭弹窗
              emit('list')
            });
        //   }
        }
      });
		};
    const resetForm = ()=>{
      state.ruleForm = {
        id:0,
        up_id: 0, // 上级部门
        name: '', // 部门名称

      }
    };
		return {
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
      formRef,
			...toRefs(state),
		};
	},
});
</script>
